//
//  singlePage.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/16.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "singlePage.h"
#import "collectionViewOfSingle.h"
#import "cellOfSinglePage.h"
#import "layoutSinglePage.h"
#import "NetWorkingHandler.h"
#import "modelOfSingle.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "singleNextPageViewController.h"

@interface singlePage ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain)collectionViewOfSingle *singleCollection;

@property (nonatomic, retain)layoutSinglePage *layoutSingle;

@property (nonatomic, retain) NSMutableArray *arrOfSingle;

@property (nonatomic, retain) NSMutableArray *arr;

@property (nonatomic, retain) NSMutableDictionary *dic2;


@end

@implementation singlePage

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"单品";
    
    
    
    self.arrOfSingle = [NSMutableArray array];
    
    
    [self creatCollectionSingle];
    [self handleSingle];
    [self refresh];
    
    

}


#pragma mark - 页面布局
- (void)creatLayout{
    
    self.layoutSingle = [[layoutSinglePage alloc]init];

}
- (void)creatCollectionSingle{
    [self creatLayout];
    self.singleCollection = [[collectionViewOfSingle alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ) collectionViewLayout:self.layoutSingle];
    
    [self.view addSubview:self.singleCollection];
    
    self.singleCollection.delegate = self;
    self.singleCollection.dataSource = self;
    
    self.singleCollection.backgroundColor = [UIColor whiteColor];
    
    [self.singleCollection registerClass:[cellOfSinglePage class] forCellWithReuseIdentifier:@"pool"];
    


}
#pragma mark - 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arrOfSingle.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    cellOfSinglePage *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    modelOfSingle *model1 = _arrOfSingle[indexPath.item];
   [cell.imageOfSingle sd_setImageWithURL:[NSURL URLWithString:model1.cover_image_url]];
    
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        _singleCollection.backgroundColor = [UIColor silverColor];
        
        
    }else{
        
        _singleCollection.backgroundColor = [UIColor whiteColor];
        
        
    }
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [[UIColor grayColor]CGColor];
    
    cell.labelOfMid.text = model1.name;
    cell.labelOfLeft.text = model1.price;
    NSString *str = [NSString stringWithFormat:@"%ld",model1.favorites_count];
    cell.labelOfRight.text = str;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    singleNextPageViewController *singleNext = [[singleNextPageViewController alloc]init];
    NSMutableArray *arr1 = [NSMutableArray array];
    NSMutableArray *arr2 = [NSMutableArray array];
    NSMutableArray *arr3 = [NSMutableArray array];
    
    for (NSDictionary *dic in self.arr) {
        
        [arr1 addObject:[dic objectForKey:@"data"]];
    }
    for (NSDictionary *dic2 in arr1) {
        
        [arr2 addObject:[dic2 objectForKey:@"url"]];
        [arr3 addObject:[dic2 objectForKey:@"name"]];
        
        
        
    }
    singleNext.url2 = arr2[indexPath.item];
    singleNext.title1 = arr3[indexPath.item];
    
    
    
    [self.navigationController pushViewController:singleNext animated:YES];
    

}
#pragma mark - 解析数据
- (void)handleSingle{
  
    
    [NetWorkingHandler handleDataWithUrl:@"http://api.bohejiaju.com/v2/items?gender=1&generation=1&limit=20&offset=0" completion:^(id result, NSURLResponse *response, NSError *error) {


        self.arr = result[@"data"][@"items"];
        for (NSDictionary *dic in self.arr) {
            
            NSDictionary *dic1 = dic[@"data"];
            modelOfSingle *model = [[modelOfSingle alloc]init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.arrOfSingle addObject:model];
            
        }
        
        [_singleCollection reloadData];
        
}];
    [_singleCollection.mj_header endRefreshing];
 }
- (void)setLightDay{
    
    NSLog(@"%s", __func__);
    [NightManager setBackgroundColorWithView:self.view];
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [_singleCollection reloadData];
    
}
- (void)setNightDay{
    NSLog(@"%s", __func__);
    [NightManager setBackgroundColorWithView:self.view];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [_singleCollection reloadData];
    
}
- (void)refresh{
    _singleCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleSingle)];
    [_singleCollection.mj_header beginRefreshing];
    
}


@end
